<template>
  <el-container class="common-layout">
    <el-header style="background: transparent; box-shadow: none; padding: 0;">
      <NavBar />
    </el-header>
    <el-main v-loading="loading">
      <div class="three-columns">
        <!-- 左侧统计卡片 -->
        <div class="left-card">
          <el-card class="stat-card">
            <div class="stat-title">大家总文章数</div>
            <div class="stat-value">{{ list.length }}</div>
          </el-card>
        </div>
        <!-- 中间内容：竖排诗文，遇标点换列，文字浮动 -->
        <div class="center-content">
          <div
            v-for="(item, idx) in list"
            :key="item.id || idx"
            class="vertical-article"
            :ref="el => articleRefs[idx] = el"
            @mouseenter="hoverIndex = idx"
            @mouseleave="hoverIndex = -1"
            :class="{ active: hoverIndex === idx }"
          >
            <div class="vertical-text" :style="{ fontFamily: fontFamily }">
              <div
                v-for="(col, cidx) in splitByPunctuation(item.content)"
                :key="cidx"
                class="column"
              >
                <span
                  v-for="(char, i) in col"
                  :key="i"
                  class="floatY"
                  :style="{ animationDelay: (i * 0.2) + 's' }"
                >{{ char }}</span>
              </div>
            </div>
          </div>
        </div>
        <!-- 右侧时间线 -->
        <div class="right-timeline">
          <el-timeline style="max-width: 300px; max-height: 80vh; overflow-y: auto; padding-right: 10px;">
            <el-timeline-item
              v-for="(item, idx) in list"
              :key="item.id || idx"
              :timestamp="formatDateTime(item.createTime) || '无时间'"
              type="primary"
            >
              <span class="timeline-link" @click="scrollToArticle(idx)">
                {{ item.title || '无标题' }}
              </span>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import NavBar from '@/components/NavBar.vue'
import request from '@/request/request'
import { ElMessage } from 'element-plus'

const fontFamily = "'STKaiti', 'KaiTi', 'FZShuTi', '宋体', serif"
const list = ref([])
const hoverIndex = ref(-1)
const articleRefs = ref([])
const loading = ref(false)

// 时间格式化函数
function formatDateTime(str) {
  if (!str) return ''
  return str.replace('T', ' ').replace(/\..*$/, '').replace('Z', '')
}

function splitByPunctuation(text) {
  const result = [];
  let temp = '';
  for (let char of text) {
    temp += char;
    // 支持更多分列符号：中文逗号、顿号、冒号、换行等
    if (/[。！？；，,.!?:、\n]/.test(char)) {
      result.push(temp);
      temp = '';
    }
  }
  if (temp) result.push(temp);
  return result;
}

function scrollToArticle(idx) {
  nextTick(() => {
    const el = articleRefs.value[idx]
    if (el) {
      el.scrollIntoView({ behavior: 'smooth', block: 'center' })
    }
  })
}

async function fetchArticles() {
  loading.value = true
  try {
    const res = await request.get('/WanZhang/all')
    list.value = res.data
  } catch (e) {
    console.error('获取文章失败:', e)
    ElMessage.error(e.message || '获取文章失败，请检查网络连接或联系管理员')
    list.value = [
      { id: 1, title: '示例文章', content: '有些人心如草木，皆向阳而生。', createTime: '2024-06-01T12:00:00' }
    ]
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchArticles()
})
</script>

<style scoped>
.common-layout {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(120deg, #e9edf1 0%, #bdbdbd 100%);
  overflow: hidden;
}
.three-columns {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin-top: 50px;
}
.left-card {
  width: 220px;
  min-width: 160px;
  margin-right: 60px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  position: fixed;
  left: 40px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}
.stat-card {
  width: 220px;
  margin-bottom: 24px;
  text-align: center;
}
.stat-title {
  font-size: 18px;
  color: #888;
  margin-bottom: 8px;
}
.stat-value {
  font-size: 32px;
  font-weight: bold;
  color: #43a047;
}
.center-content {
  flex: 1;
  max-width: 520px;
  min-width: 300px;
  margin: 0 100px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.vertical-article {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin: 100px 0 100px 0;
  position: relative;
  cursor: pointer;
  transition: box-shadow 0.2s, background 0.2s;
}
.vertical-article.active {
  box-shadow: 0 0 0 2px #43a04733;
  background: #f6f7fb;
  border-radius: 12px;
}
.vertical-text {
  display: flex;
  flex-direction: row;
  gap: 32px;
  writing-mode: initial;
  text-align: center;
  font-size: 38px;
  color: #222;
  letter-spacing: 6px;
  text-shadow: 2px 2px 8px #aaa;
  font-family: 'STKaiti', 'KaiTi', 'FZShuTi', '宋体', serif;
  background: transparent;
}
.column {
  writing-mode: vertical-rl;
  text-align: center;
}
.floatY {
  display: inline-block;
  animation: floatY 2.5s ease-in-out infinite;
}
@keyframes floatY {
  0% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
  100% { transform: translateY(0); }
}
.right-timeline {
  width: 260px;
  min-width: 160px;
  margin-left: 60px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  position: fixed;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}
.timeline-link {
  cursor: pointer;
  color: #409eff;
  text-decoration: none;
}
.timeline-link:hover {
  color: #66b1ff;
}
@media (max-width: 1200px) {
  .three-columns {
    flex-direction: column;
    align-items: stretch;
  }
  .left-card, .right-timeline {
    position: static;
    width: 100%;
    min-width: unset;
    margin: 0 0 24px 0;
    align-items: center;
    top: unset;
    left: unset;
    right: unset;
    transform: unset;
  }
  .center-content {
    margin: 0 0 24px 0;
    max-width: 100%;
  }
}
</style>
